package com.totcy.atom.utils.ImageGetter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.view.View;

import com.totcy.atom.view.activity.PictureBrowsingActivity;

import org.xml.sax.XMLReader;

import java.util.ArrayList;
import java.util.Locale;

public class DetailTagHandler implements Html.TagHandler {
    private Context context;
    private ArrayList<String> strings = new ArrayList<>();

    public DetailTagHandler(Context context) {
        this.context = context;
    }

    @Override
    public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
        // 处理标签<img>
        if ("img".equals(tag.toLowerCase(Locale.getDefault()))) {
            // 获取长度
            int len = output.length();
            // 获取图片地址
            ImageSpan[] images = output.getSpans(len - 1, len, ImageSpan.class);
            String imgURL = images[0].getSource();
            // 记录所有图片地址
            imgURL = imgURL.replace("images", "http://rk.isoft365.com/rkimages");
            strings.add(imgURL);
            // 记录是第几张图片
            int position = strings.size() - 1;
            // 使图片可点击并监听点击事件
            output.setSpan(new ClickableImage(context, position), len - 1, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }

    private class ClickableImage extends ClickableSpan {
        private Context context;
        private int position;

        public ClickableImage(Context context, int position) {
            this.context = context;
            this.position = position;
        }

        @Override
        public void onClick(View widget) {

            Intent intent = new Intent(context, PictureBrowsingActivity.class);
            intent.putStringArrayListExtra("urlList", strings);
            intent.putExtra("position", position);
            context.startActivity(intent);
        }
    }
}
